#include "button.h"
#include "SDL_mixer.h"
#include "game_engine.h"

Button::Button(std::string imageSet, ButtonCallback callbackFunc, void *callbackObj)
	: RenderTarget(imageSet)
{
	getIdComp()->addTag("button"); //Add button tag so I can be found by that

	state = BUTTON_NORMAL;

	relativeToParent = true;
	callback = callbackFunc;
	callbackObject = callbackObj;
	hotKey = SDLK_UNKNOWN;

	buttonSprite = new SpriteSheet(imageSet, true, 3);
	buttonSprite->centered = true;
	buttonSprite->setZOrder(-100);
	buttonSprite->setRelativeToParent(true);
	addChild(buttonSprite);
}


bool Button::IsOverlapping(int mX, int mY)
{
	if (buttonSprite != NULL)
	{
		Sint16 x, y;
		buttonSprite->getScreenCoords(x, y);
		PD_rect bounds;
		bounds.setTopLeft(x, y);
		bounds.setSize( buttonSprite->frameOffset, buttonSprite->getRenderImage()->h);
		
		PD_rect clickBounds;
		clickBounds.setAll(mX - 0.5, mY - 0.5, 1.0, 1.0);
		return bounds.collidesWith(clickBounds);
	}
	return false;
}


bool Button::OnLButtonDown(int mX, int mY)
{
	if (state == BUTTON_DISABLED) return false;
	if (IsOverlapping(mX, mY))
	{
		state = BUTTON_PRESSED;
		buttonSprite->frame = 1;
		return true;
	}
	return false;
}

bool Button::OnLButtonUp(int mX, int mY)
{
	if (state == BUTTON_DISABLED) return false;
	if (IsOverlapping(mX, mY))
	{
		state = BUTTON_NORMAL;
		buttonSprite->frame = 0;

		Mix_Chunk* click = GameEngine::getResources()->GetSound("Pick Sound.wav");
		if (click != NULL)
		{
			Mix_PlayChannel(-1, click, 0);
		}

		if (callback != NULL)
			(*callback)(callbackObject, this);

		return true;
	}
	return false;
}

bool Button::OnMouseMove(int mX, int mY, int relX, int relY, bool Left,bool Right,bool Middle)
{
	if (state == BUTTON_DISABLED) return false;

	if (buttonSprite != NULL)
	{
		if (state == BUTTON_NORMAL)
		{
			if (IsOverlapping(mX, mY))
			{
				state = BUTTON_HOVER;
				buttonSprite->frame = 2;
			}
		}
		else if (state == BUTTON_PRESSED)
		{
			if (!IsOverlapping(mX, mY))
			{
				state = BUTTON_NORMAL;
				buttonSprite->frame = 0;
			}
		}
		else if (state == BUTTON_HOVER)
		{
			if (!IsOverlapping(mX, mY))
			{
				state = BUTTON_NORMAL;
				buttonSprite->frame = 0;
			}
		}
	}
	return false;
}

bool Button::OnKeyDown(SDLKey sym, SDLMod mod, Uint16 unicode)
{
	if (state == BUTTON_DISABLED) return false;

	if (sym == hotKey)
	{
		/*
		state = BUTTON_PRESSED;
		buttonSprite->frame = 1;
		return true;
		*/
		//state = BUTTON_NORMAL;
		//buttonSprite->frame = 0;

		Mix_Chunk* click = GameEngine::getResources()->GetSound("Pick Sound.wav");
		if (click != NULL)
		{
			Mix_PlayChannel(-1, click, 0);
		}

		if (callback != NULL)
			(*callback)(callbackObject, this);

		return true;

	}
	return false;
}

bool Button::OnKeyUp(SDLKey sym, SDLMod mod, Uint16 unicode)
{
	/*
	if (state == BUTTON_DISABLED) return false;
	
	if (sym == hotKey)
	{
		state = BUTTON_NORMAL;
		buttonSprite->frame = 0;

		Mix_Chunk* click = GameEngine::getResources()->GetSound("Pick Sound.wav");
		if (click != NULL)
		{
			Mix_PlayChannel(-1, click, 0);
		}

		if (callback != NULL)
			(*callback)(callbackObject, this);

		return true;
	}
	*/
	return false;
}

void Button::SetEnabled(bool enable)
{
	if (state != BUTTON_DISABLED && !enable)
	{
		state = BUTTON_DISABLED;
		buttonSprite->frame = 1;
	}
	else if (state == BUTTON_DISABLED && enable)
	{
		state = BUTTON_NORMAL;
		buttonSprite->frame = 0;
	}
}

bool Button::GetEnabled()
{
	return state != BUTTON_DISABLED;
}

void Button::staticEnableButton(void *obj, Timer *timer)
{
	if (obj != NULL)
	{
		Button *button = (Button*)obj;
		button->SetEnabled(true);
	}
}
